import config from "@/config/config.js"
import store from "@/store/index.js"
import request from "@/config/TokenRequst.js"

const uploadFile = (path) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			name: "file",
			url: "http://" + config.url + '/file/upload',
			filePath: path,
			header: {
				token: uni.getStorageSync("token")
			},
			success(res) {
				let data = JSON.parse(res.data)
				resolve(data.data.url);
			},
			fail(e) {
				reject(e);
			}
		})
	})
}
const mapToJson = (map) => {
	return JSON.stringify([...map]);
}

const jsonToMap = (jsonStr) => {
	if (jsonStr == "" || jsonStr == undefined || jsonStr == null)
		return new Map();
	else
		return new Map(JSON.parse(jsonStr));
}

//查找聊天列表里的id ,没有则添加
const findIdAndAdd = (uid) => {
	// console.log(uid)
	// console.log(store.state.chatList)
	// for (var i = 0; i < store.state.chatList.length; i++) {
	// 	if (store.state.chatList[i].user.uid === uid) {
	// 		return i;
	// 	}
	// }
	if (store.state.singleChatMap.has(parseInt(uid))) {
		let index = store.state.singleChatMap.get(parseInt(uid))
		console.log("map Has uid " + uid + " in :" + index)
		return index
	}
	let user = {
		uid: uid,
		avatar: "@/static/logo.png",
		username: "加载中..."
	}
	let data = {
		user: user,
		unRead: 0,
		messageList: []
	}
	store.state.chatList.push(data)
	let index = store.state.chatList.length - 1
	store.state.singleChatMap.set(parseInt(uid), parseInt(index))
	console.log("set id:" + uid + " in index:" + index)
	console.log("findIdAndAdd请求好友信息")
	request.TokenRequest({
		url: "/user/getUserInfoByUid"
	}, {
		uid: uid
	}).then(res => {
		store.state.chatList[index].user = res.data
	})
	return index;
}


const add0 = (m) => {
	return m < 10 ? '0' + m : m
}


const convertTime = (timestamp) => {
	var time = new Date(timestamp);
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y + "/" + m + "/" + d + " " + add0(h) + ":" + add0(mm) + ":" + add0(s);
}
const emoList = [
	[{
			lable: '[爱你]',
			value: 'aini'
		},
		{
			lable: '[奥特曼]',
			value: 'aoteman'
		},
		{
			lable: '[拜拜]',
			value: 'baibai'
		},
		{
			lable: '[抱抱]',
			value: 'baobao'
		},
		{
			lable: '[悲伤]',
			value: 'beishang'
		},
		{
			lable: '[并不简单]',
			value: 'bingbujiandan'
		},
		{
			lable: '[鄙视]',
			value: 'bishi'
		},
		{
			lable: '[闭嘴]',
			value: 'bizui'
		},
		{
			lable: '[馋嘴]',
			value: 'chanzui'
		},
		{
			lable: '[吃惊]',
			value: 'chijing'
		},
		{
			lable: '[打哈欠]',
			value: 'dahaqi'
		},
		{
			lable: '[打脸]',
			value: 'dalian'
		},
		{
			lable: '[敲头]',
			value: 'ding'
		},
		{
			lable: '[狗头]',
			value: 'doge'
		},
		{
			lable: '[二哈]',
			value: 'erha'
		},
		{
			lable: '[费解]',
			value: 'feijie'
		},
		{
			lable: '[肥皂]',
			value: 'feizao'
		},
		{
			lable: '[感冒]',
			value: 'ganmao'
		},
		{
			lable: '[鼓掌]',
			value: 'guzhang'
		},
		{
			lable: '[哈哈]',
			value: 'haha'
		},
		{
			lable: '[害羞]',
			value: 'haixiu'
		},
		{
			lable: '[汗]',
			value: 'han'
		},
		{
			lable: '[呵呵]',
			value: 'hehe'
		},
		{
			lable: '[嘿嘿嘿]',
			value: 'heiheihei'
		}
	],
	[{
			lable: '[黑线]',
			value: 'heixian'
		},
		{
			lable: '[哼]',
			value: 'heng'
		},
		{
			lable: '[坏笑]',
			value: 'huaixiao'
		},
		{
			lable: '[花心]',
			value: 'huaxin'
		},
		{
			lable: '[急眼]',
			value: 'jiyan'
		},
		{
			lable: '[可爱]',
			value: 'keai'
		},
		{
			lable: '[可怜]',
			value: 'kelian'
		},
		{
			lable: '[酷]',
			value: 'ku'
		},
		{
			lable: '[骷髅]',
			value: 'kulou'
		},
		{
			lable: '[困]',
			value: 'kun'
		},
		{
			lable: '[懒得理你]',
			value: 'landelini'
		},
		{
			lable: '[泪]',
			value: 'lei'
		},
		{
			lable: '[怒]',
			value: 'nu'
		},
		{
			lable: '[怒骂]',
			value: 'numa'
		},
		{
			lable: '[钱]',
			value: 'qian'
		},
		{
			lable: '[亲亲]',
			value: 'qinqin'
		},
		{
			lable: '[傻眼]',
			value: 'shayan'
		},
		{
			lable: '[生病]',
			value: 'shengbing'
		},
		{
			lable: '[失望]',
			value: 'shiwang'
		},
		{
			lable: '[衰]',
			value: 'shuai'
		},
		{
			lable: '[睡觉]',
			value: 'shuijiao'
		},
		{
			lable: '[思考]',
			value: 'sikao'
		},
		{
			lable: '[太开心]',
			value: 'taikaixin'
		},
		{
			lable: '[摊手]',
			value: 'tanshou'
		}
	],
	[{
			lable: '[舔]',
			value: 'tian'
		},
		{
			lable: '[偷笑]',
			value: 'touxiao'
		},
		{
			lable: '[吐]',
			value: 'tu'
		},
		{
			lable: '[挖鼻孔]',
			value: 'wabishi'
		},
		{
			lable: '[委屈]',
			value: 'weiqu'
		},
		{
			lable: '[污]',
			value: 'wu'
		},
		{
			lable: '[笑哭]',
			value: 'xiaoku'
		},
		{
			lable: '[星星眼]',
			value: 'xingxingyan'
		},
		{
			lable: '[嘻嘻]',
			value: 'xixi'
		},
		{
			lable: '[嘘]',
			value: 'xu'
		},
		{
			lable: '[阴险]',
			value: 'yinxian'
		},
		{
			lable: '[疑问]',
			value: 'yiwen'
		},
		{
			lable: '[右哼哼]',
			value: 'youhengheng'
		},
		{
			lable: '[晕]',
			value: 'yun'
		},
		{
			lable: '[允悲]',
			value: 'yunbei'
		},
		{
			lable: '[抓狂]',
			value: 'zhuakuang'
		}
	]
]


export default {
	convertTime,
	emoList,
	uploadFile,
	add0,
	findIdAndAdd,
	mapToJson,
	jsonToMap
}
